package service;

import java.util.List;
import java.util.Map;

import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionContext;

import dao.PlaceDao;
import dao.TutorDao;
import po.EmpHistory;
import po.Order;
import po.User;

public class TutorService {
	private Map<String, Object> request, session;
	private List list;
	//private List emp;
	//接受预约
	public boolean accept(int id) {
		//传递信息
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		TutorDao dao = new TutorDao();
		
		Object obj = dao.find(id);
		Order ord = (Order)obj;
		
		ord.setStatus(2); //将订单状态设置为2-进行中
		
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.updateStatus(ord);;
			tran.commit();
			request.put("tip", "成功接受订单！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	//拒绝预约
	public boolean refuse(int id) {
		//传递信息
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		TutorDao dao = new TutorDao();
		
		Object obj = dao.find(id);
		Order ord = (Order)obj;
		
		ord.setStatus(0); //订单状态恢复
		ord.setCostomerID(null); //预设客户抹去
		
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.updateStatus(ord);
			tran.commit();
			request.put("tip", "用户的申请已被取消！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	//课程教授完毕-当用户发起结束订单时，选择确认结束得到报酬 订单状态更新到4
	public boolean finishToCourse(int id) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		TutorDao dao = new TutorDao();
		
		Object obj = dao.find(id);
		Order ord = (Order)obj;
		EmpHistory history = new EmpHistory(ord);
		
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.delete(ord);
			dao.saveHistory(history);
			
			tran.commit();
			request.put("tip", "该订单已结束！");  
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	//完善关联信息
	public void setParam(Order ord) {
		int id = ord.getPromulgatorID();
		TutorDao dao = new TutorDao();
		Object obj = dao.findInfo(id);
		User user = (User)obj;
		ord.setName(user.getUserDetail().getName());
		ord.setSex(user.getUserDetail().getSex());
		ord.setBackground(user.getUserDetail().getBackground());
	}
	
	//发布课程
	public boolean issueCourse(Order ord) {
		//传递信息
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		TutorDao dao = new TutorDao();
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.issue(ord);
			tran.commit();
			request.put("tip", "课程已成功发布！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	//删除课程
	public boolean deleteCourse(Order ord) {
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		
		TutorDao dao = new TutorDao();
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.delete(ord);
			tran.commit();
			request.put("tip", "课程已成功删除！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
	}
	//修改课程信息
	public boolean modCourse(int ordID,String money,String date,String address,String text) {
		//由订单id得到订单实体
		TutorDao dao = new TutorDao();
		Order o = (Order)dao.find(ordID);	
		//修改订单实体信息
		if(money.trim().length()>0) {
			o.setPrice(Double.parseDouble(money));
		}
		if(date.trim().length()>0) {
			o.setDate(date);
		}
		if(address.trim().length()>0) {
			o.setAddress(address);
		}
		if(text.trim().length()>0) {
			o.setNeedOrPresentation(text);
		}
		
		//update
		Transaction tran = null;
		try {
			tran = dao.getSession().beginTransaction();
			dao.updateStatus(o);
			tran.commit();
			request.put("tip", "课程已成功修改！");
			return true;
		} catch (Exception e) {
			if(tran != null) tran.rollback();
			return false;
		} finally {
			dao.getSession().close();
		}
		 
	}
	
	//对用户的招聘信息发起申请
	public Boolean place(int userID,int orderID) {
		//对用户的招聘信息发起申请
		String hql = "update Employment o set o.promulgatorID = "+userID+"  ,o.status = 1 where o.employmentID = "+orderID;
		PlaceDao dao = new PlaceDao();
		dao.update(hql);
		return true;
	}
	
	//得到该家教账户下申请中和进行中的订单 订单状态为1和2
	public Boolean getOrders(int id) {
		String hql = "from Employment as emp where promulgatorID = "+id+" and status in(1,2)";
		PlaceDao dao = new PlaceDao();
		list = dao.getOrders(hql);
		if(list.isEmpty()) {
			return false;
		}
		return true;
	}	
	
	//家教取消对招聘信息的申请
	public Boolean cancel(int orderID) {
		//对用户的招聘信息发起申请
		String hql = "update Employment o set o.promulgatorID = null"+"  ,o.status = 0 where o.employmentID = "+orderID;
		PlaceDao dao = new PlaceDao();
		dao.update(hql);
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		request.put("info", "取消申请成功！");
		return true;
	}
	
	//家教确认接受的招聘完成
	public Boolean finish(int orderID) {
		String hql = "update Employment o set o.status = 3 where o.employmentID = "+orderID;
		PlaceDao dao = new PlaceDao();
		dao.update(hql);
		ActionContext ctx= ActionContext.getContext();
		session=(Map) ctx.getSession();
		request=(Map) ctx.get("request");
		request.put("info", "已发起课程完成申请，请等待用户确认！");
		return true;
	}
	
	public List getList() {
		return this.list;
	}
}
